package tables

import (
	"peilian-api/app/global/variable"

	"gorm.io/gorm"
)

var (
	PIC_TYPE       = "pic_type"
	VIDEO_TYPE     = "video_type"
	SATISFIED_TYPE = "satisfied_type"
	ADVICE_TYPE    = "advice_type"
	PROBLEM_TYPE   = "problem_type"
)

var VpDataTypeMap = map[string]string{
	"1": PIC_TYPE,
	"2": VIDEO_TYPE,
	"3": SATISFIED_TYPE,
	"4": ADVICE_TYPE,
	"5": PROBLEM_TYPE,
}

type Vpdata struct { //数据表
	gorm.Model
	UserId   uint   `gorm:"type:uint;not null;comment:用户id"`
	Sid      uint   `gorm:"type:uint;default:0;comment:数据id,可有可无"`
	ExpandId uint   `gorm:"type:uint;default:0;comment:数据扩展id,可有可无"`
	Info     string `gorm:"type:varchar(512);comment:反馈内容"`
	Type     string `gorm:"type:varchar(20);comment:数据类型,pic_type:图片 video_type:视频 satisfied_type:满意度打分 advice_type:建议 problem_type:问题"`
}

func (u *Vpdata) TableName() string {
	return "vpdata"
}

func (v *Vpdata) InsertVp(vp Vpdata) (int64, error) {
	d := variable.DB.MySQL.Model(v).Save(&vp)
	if d.Error != nil {
		return 0, d.Error
	}
	return int64(vp.ID), nil
}

func (v *Vpdata) UpsertVp(vp Vpdata) (string, error) {
	var vpData Vpdata
	result := variable.DB.MySQL.Model(v).Where("user_id = ? and sid = ? and expand_id = ? and type = ? ", vp.UserId, vp.Sid, vp.ExpandId, vp.Type).First(&vpData)
	if result.Error != nil {
		if result.Error == gorm.ErrRecordNotFound {
			result := variable.DB.MySQL.Model(v).Create(&vp)
			if result.Error != nil {
				return "insert error", result.Error
			}
			return "insert", nil
		}
	} else {
		result := variable.DB.MySQL.Model(v).Where("user_id = ? and sid = ? and expand_id = ? and type = ?", vp.UserId, vp.Sid, vp.ExpandId, vp.Type).Updates(map[string]interface{}{"info": vp.Info})
		if result.Error != nil {
			return "update error", result.Error
		}
		return "update", nil
	}
	return "", nil
}
